package com.yanqu.road.server.cmd.activity.xiaoyucostume;

import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.bag.BagProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityMallModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

/**
 * @author ：zfr
 * @date ：2021/12/30 16:44
 */
@Cmd(code = GameProtocol.S_XIAOYU_COSTUME_ACTIVITY_USE_GOOD, desc = "小玉服装活动使用道具")
public class XiaoyuCostumeActivityUseGoodsCmd extends AbstractCommand {
    private int goodsId;

    private long count;

    private long param;

    private UseGoodsResult result = null;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BagProto.BagUseGoodsReqMsg reqMsg = BagProto.BagUseGoodsReqMsg.parseFrom(packet.getBytes());
        goodsId = reqMsg.getGoodsId();
        count = reqMsg.getCount();
        param = reqMsg.getParam();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        BagModule bagModule = player.getModule(BagModule.class);
        UserBagItem bagItem = bagModule.getUserBagItem(goodsId);
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(goodsId);
        if (null == goodsInfo) {
            ret = GameErrorCode.E_GOODS_NO_EXIST;
            return;
        }
        if (bagItem == null || bagItem.getNum() <= 0 || bagItem.getNum() < goodsInfo.getParamList().get(0).longValue()) {
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        result = player.getModule(ActivityMallModule.class).useXiaoyuCostumeActivityGoods(goodsInfo);
    }

    @Override
    public void sendData(GamePlayer player) {
        BagProto.BagUseGoodsRespMsg.Builder respMsg = BagProto.BagUseGoodsRespMsg.newBuilder();
        respMsg.setRet(ret);
        respMsg.setGoodsId(goodsId);
        respMsg.setCount(count);
        if (0 == ret) {
            if (null != result) {
                respMsg.setRet(result.getRet());
                respMsg.setReward(result.getReward());
            } else {
                respMsg.setReward("");
            }
        }

        if (result == null) {
            respMsg.setSpecialReward(false);
        } else {
            respMsg.setSpecialReward(result.isSpecialReward());
        }

        player.sendPacket(Protocol.U_XIAOYU_COSTUME_ACTIVITY_USE_GOOD, respMsg);
    }
}
